function analysis(varargin)
% Analysis.m - plugin analysis template, Jan 2007
%
% Analysis is a template for data analysis routines to be used in the datac
% analysis program. This template shows how to write an analysis function,
% and how to allow the function to do an analysis on records selected in
% the main database window. 
%%
% The template can be used as a starting point. If you change this, first
% change the name of the function as listed above to something reasonable. 
% You do not have to change the name of the local function 'analysis2'.
% This code is more extensively documented than usual, to help you
% understand how it works. When you make your own routines, you may wish to
% remove some of these comments.
%  
% Paul B. Manis, Ph.D. pmanis@med.unc.edu
%
% Variable and function descriptions:
% nargin: nargin is the count of the number of arguments that appeared 
% in the function call to 'analysis'. nargin is provided by matlab when we
% call the routine. We use the 'varargin' (variable
% argument input list) for flexibility in writing the analysis routine so
% that we can call it from the command line for testing, or use it as a
% "plugin" with no arguments to do automated analysis.
%
% varargin: This is the matlab name for a variable argument list. A
% variable argument list is a cell array. Cell arrays can hold any data
% type, so you can mix text, numbers and structures (or other cell arrays)
% in the call. The array is indexed according to the position of the
% argument on the calling command line. 
% 
% test_mode: this is a flag that will be checked in the local routine. If
% it is 0, we access the data in the database for the analysis. If it is
% not, we use it as a directive to generate test data to verify the
% function of the routine. 
%
% getmainselection: This is a function that we provide outside this
% function, to return the indices of the selected elements of the database
% (e.g., the lines in the main screen of datac when the database is shown).
% The return is a one-dimensional numeric array. Typically, I call this
% array 'sf' for 'selection'. 
%
% getplotflag: This is a function that we provide outside this function. It
% reads the status of the checkbox on the datac window labeled "plot".
% You can use this flag to control whether the data is plotted on the
% display, or more frequently, whether it is sent automatically to the
% default printer during analysis. This allows you to run the analysis for
% test purposes without printing it, and then to rerun it with the printer
% enabled when everything is working so you can have hardcopy.
%
% analysis2: this is the routine that does the work. Functions called in
% 'analysis2' are described therein.

if(nargin == 0) % check for arguments in the call
    test_mode = 0; % if none, provide default values
else
	test_mode = varargin{1}; % arguments are in a cell array - extract them
end;
sf = getmainselection; % call the routine that lists the selected record indices
   if(sf > 0) % make sure there's a selection
      pflag = getplotflag; % also get the plot flag checkbox status
      QueMessage('Analysis', 1); % clear the que and show a message
      for i = 1:length(sf) % loop through the selected database entries
         analysis2(sf(i), pflag, test_mode); % call the analysis routine with argumetns
      end;
   end;

return;

   
%------------------------------------------------------
   
function analysis2(sf, plot_flag, n)
% analysis2
%
% This routine does the actual work.  
% The global variables below are used to acces the data and data structures
% in datac. ALLCH is a cell array matrix that holds the data for all channels.
% You really only need to access it if you have more than two channels, as 
% same data for the first two channels is held in the VOLTAGE and CURRENT
% arrays (under the control of the mode flag - whether current or voltage
% clamp). 
% DFILE is a structure that contains information about the most recently 
% read data records. Some of the information is reduntantly stored in the
% CONTROL structure also.
% CONTROL is the central database of the whole datac program. CONTROL is an
% sturctured array. Each element of the array corresponds to an entry in
% the database - usually a group of records or a "block" of data from a
% cell. The elements of CONTROL contain information about the file for the
% data, the records, the gains, analysis parameters, and it can contain
% result information. The CONTROL structure is saved as the database. You
% can add fields to the CONTROL structure to store unique results. 
%
% functions called:
%
% analysis_setup: This routine accesses the file pointed to by the selected
% (sf) record. It compares the current DFILE information with the database
% information in CONTROL(sf), and decides whether it needs to load a
% different set of data into the ALLCH, VOLTAGE and CURRENT arrays. If it
% does, it attempts to do so. If it is successful (finds the data and reads
% it without error), the return variable err will be 0, and DFILE will
% contain the new information about the data. If it fails for any reason
% (the data is not found at the path identified, or the file is corrupt),
% then err will be 1 and the program will return. DFILE will not be
% changed.
%
% analysis_setup2: This routine does common parsing of paramteres from the
% data and the stimulus structure. This parsing used to be done in-line,
% but it is much cleaner to just include it here. Note that the returned
% variables are just listed out. 
%
% QueMessage: this routine prints a message to the mesage window of datac. 

%
%

%
global ALLCH  VOLTAGE CURRENT
global DFILE  CONTROL

    [DFILE, err] = analysis_setup(DFILE, sf);
    if(err ~= 0)
        return;
    end;

    [protocol, rate, records, pts, frec, lrec, time, TM, ZT, TL, VL] = analysis_setup2(DFILE, sf);
    QueMessage('Analysis Setup done');

% end of required code to read in the data and get parameters.

tsw1 = 5;
tsw2 = 250;
spike_thresh = 0;

[stim, df] = block_info(sf);

QueMessage('Analysis - Finding spikes');
[first_spike, first_isi, nr_spikes, spike_train]=find_spikes(DFILE, tsw1, tsw2, spike_thresh);

a=mean(first_spike);
b=std(first_spike);

fprintf(1, 'sf: %d  fsl: %8.3f  stdev: %8.3f\n', sf, a, b)

